﻿using B.S.Data.MES.API.Applications.WorkOrderManagement.Commands;
using B.S.Domain.Ticket;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.Data.MES.API.Applications.WorkOrderManagement.CommandHandlers
{
    public class CreateWorkOrderCommandHandler : IRequestHandler<CreateWorkOrderCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<WorkOrder> _workorderRepository;
        public CreateWorkOrderCommandHandler(IBaseRepository<WorkOrder> workorderRepository)
        {
            _workorderRepository = workorderRepository;
        }
        public async Task<APIRequst<int>> Handle(CreateWorkOrderCommand request, CancellationToken cancellationToken)
        {
            WorkOrder work = new WorkOrder();
            work.WorkCode = YitIdHelper.NextId().ToString();
            work.Type = request.Type;
            work.CustomerId = request.CustomerId;
            work.ProductId = request.ProductId;
            work.ProcessroutId = request.ProcessroutId;
            work.ProductionlineId = request.ProductionlineId;
            work.StartTime = request.StartTime;
            work.EndTime = request.EndTime;
            work.WorkNum = request.WorkNum;
            work.IsSkipprocess = request.IsSkipprocess;
            work.Scoure=request.Scoure;
            work.States = request.States;
            work.DingDanId=request.DingDanId;
            work.Remark = request.Remark;
            work.CreateName = request.CreateName;
            work.CreateTime = DateTime.Now;

            await _workorderRepository.Add(work);
            return await Task.FromResult(new APIRequst<int>
            {
                Code = RequstCode.成功,
                Msg=RequstCode.成功.ToString()
            });


        }
    }
}
